﻿using System;
using System.Windows.Threading;

namespace Apps.Yunwei.ViewModels
{
	public class Counter
	{
		private int currentCount = 0;
		private DispatcherTimer timer;

		public int Increment
		{
			get;
			set;
		}

		public TimeSpan Timeout
		{
			get;
			set;
		}

		public int Count
		{
			get
			{
				if (this.timer == null)
				{
					this.timer = new DispatcherTimer();
					this.timer.Interval = this.Timeout;
					this.timer.Tick += (o,e) =>
						{
							this.Reset();
						};
					this.timer.Start();
				}
				this.currentCount += this.Increment;
				return this.currentCount;
			}
		}
		
		public void Reset()
		{
			this.currentCount = 0;
			if (this.timer != null)
			{
				this.timer.Stop();
				this.timer = null;
			}
		}

		public Counter()
		{
			// Insert code required on object creation below this point.
		}
	}
}